package org.yuanqiframework.yuanqi.boot.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.web.bind.annotation.*;
import org.yuanqiframework.yuanqi.web.auth.AuthContext;
import org.yuanqiframework.yuanqi.web.controller.BaseController;
import org.yuanqiframework.yuanqi.web.entity.AjaxResult;
import org.yuanqiframework.yuanqi.core.utils.BeanUtils;
import org.yuanqiframework.yuanqi.boot.sys.dto.SysMenuDTO;
import org.yuanqiframework.yuanqi.boot.sys.entity.SysMenu;
import org.yuanqiframework.yuanqi.boot.sys.entity.SysRoleMenu;
import org.yuanqiframework.yuanqi.boot.sys.service.SysMenuService;
import org.yuanqiframework.yuanqi.boot.sys.service.SysRoleMenuService;
import org.yuanqiframework.yuanqi.boot.sys.vo.MenuVO;
import org.yuanqiframework.yuanqi.boot.sys.vo.PageMenuVO;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Set;

@Slf4j
@RestController
@RequestMapping("/api/sys/menu")
public class MenuController extends BaseController {
    @Resource
    private SysMenuService menuService;
    @Resource
    private SysRoleMenuService sysRoleMenuService;


    /**
     * 获取菜单列表
     */
    @GetMapping("/listAll")
    public AjaxResult<List<MenuVO>> listAll() {
        return success(menuService.selectMenuList());
    }


    @GetMapping("/roleMenuAll")
    public AjaxResult<List<PageMenuVO>> roleMenuAll() {
        log.info("获取角色菜单列表");
        return success(menuService.roleMenu());
    }

    @GetMapping("/roleMenu")
    public AjaxResult<?> roleMenu() {
        Set<String> roles = AuthContext.getRoles();
        if (roles == null || roles.isEmpty()) {
            return error("权限不足");
        }
        if (roles.contains("admin")) {
            return success(menuService.roleMenu());
        }
        return success(menuService.roleMenu(roles));
    }

    @GetMapping("/getRoleMenuIds/{roleId}")
    public AjaxResult<List<PageMenuVO>> getRoleMenuIds(@PathVariable String roleId) {
        return success(sysRoleMenuService.listObjs(new LambdaQueryWrapper<SysRoleMenu>()
                .select(SysRoleMenu::getMenuId)
                .eq(SysRoleMenu::getRoleId, roleId)));
    }

    @PostMapping("/updateRoleMenus/{roleId}")
    public AjaxResult<?> updateRoleMenus(@RequestBody @NotNull List<Long> ids, @PathVariable String roleId) {
        return success(sysRoleMenuService.updateRoleMenus(roleId, ids));
    }


    @PostMapping(value = "create")
    public AjaxResult<?> create(@Valid @RequestBody SysMenuDTO sysMenuDTO) {
        SysMenu sysMenu = new SysMenu();
        BeanUtils.copyProperties(sysMenuDTO, sysMenu);
        return toAjax(menuService.save(sysMenu));
    }


    @PostMapping(value = "update/{id}")
    public AjaxResult<?> update(@PathVariable Long id, @Valid @RequestBody SysMenuDTO sysMenuDTO) {
        SysMenu sysMenu = new SysMenu();
        BeanUtils.copyProperties(sysMenuDTO, sysMenu);
        sysMenu.setMenuId(id);
        return toAjax(menuService.updateById(sysMenu));
    }

    @GetMapping("queryById/{id}")
    public AjaxResult<SysMenu> queryById(@PathVariable Long id) {
        return success(menuService.getById(id));
    }

    @PostMapping("deleteById/{id}")
    public AjaxResult<?> deleteById(@PathVariable Long id) {
        return success(menuService.safeDeleteById(id));
    }
}
